<?php
/**
 * 产品属性管理 
 *
 * @copyright   Copyright 2011, Richie
 * @author      Richie <richbossman@163.com>
 * @package     SMS
 * @link        http://www.letphp.com
 * 
 * $Id: AttributeAction.class.php 62 2011-04-02 15:05:11Z 442767452@qq.com $
 */
class AttributeAction extends SMSAction {
	
	//添加属性组
	function addgroup(){
		$this->display();
	}
	
	//编辑属性组
	function editgroup(){
		$this->edit();
	}
	
	//添加属性组处理
	function insertgroup(){
		if(isset($_POST['action']) && ($_POST['action'] == 'addattr' || $_POST['action'] == 'addattrvalue')){
			if(empty($_POST['parent_id'])){
				$this->error('数据出错，请返回上一步重新发起操作。');
			}
		}
		$this->insert();
	}
	
	//编辑属性的处理
	function updategroup(){
		if(isset($_POST['action']) && ($_POST['action'] == 'editattr' || $_POST['action'] == 'editattrvalue')){
			if(empty($_POST['parent_id'])){
				$this->error('数据出错，请返回上一步重新发起操作。');
			}
		}
		$this->update();
	}
	
	//属性列表json
	function attributelist(){
		$result = array();
		$type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : 1;
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
		$where = "type=$type";
		if($id){
			$where .= " AND parent_id=$id";
		}
		$this->datagrid['sort'] = isset($_REQUEST['sort']) ? 'n.'.trim($_REQUEST['sort']) : 'n.sort';
		$result['total'] = M()->table(C('DB_PREFIX').'attribute AS n')->where($where)->count();
		$result['rows'] = M()->table(C('DB_PREFIX').'attribute AS n')->field('n.id, n.name, n.sort, n.status, n.add_time, n.remark')
				->page($this->datagrid['page'])
				->limit($this->datagrid['rows'])
				->order($this->datagrid['sort'].' '.$this->datagrid['order'])
				->where($where)
				->findAll();
		foreach ($result['rows'] as $key=>$row){
			$result['rows'][$key]['add_time'] = date('Y-m-d H:i', $row['add_time']);
			$result['rows'][$key]['status'] = $row['status'] == 1 ? '开启' : '禁用';
		}
		$this->ajaxReturn('', $result);
	}
	
	//删除属性
	function deleteAttribute(){
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
		if(!$id){
			$this->error('非法操作！');
		}
		//有子类，不能删除
		$child = D('Attribute')->field('id')->where('parent_id='.$id)->find();
		if($child){
			$this->error('该属性存在子属性，不能被删除！');
		}
		unset($child);
		//被库存使用
		$child = D('Stock')->field('id')->where('attr1_id='.$id.' OR attr2_id='.$id)->find();
		if($child){
			$this->error('库存内存在该属性的产品，不能被删除！');
		}
		unset($child);
		//被扩展属性使用
		$child = D('Pattr')->field('id')->where('attr_id='.$id)->find();
		if($child){
			$this->error('有部分产品正在使用该属性，不能被删除！');
		}
		unset($child);
		//被产品分类关联
		$child = D('Category')->field('id')->where('attr1_id='.$id.' OR attr2_id='.$id)->find();
		if($child){
			$this->error('该属性正被产品分类关联，不能被删除！');
		}
		//删除
		D('Attribute')->delete($id);
		$this->success('删除成功！');
	}
	
	//属性列表
	function attrlist(){
		$this->global_list();
		$this->display();
	}
	
	//添加属性
	function addattr(){
		$this->global_addoredit();
		$this->display();
	}
	
	//编辑属性
	function editattr(){
		$this->global_addoredit();
		$this->edit();
	}
	
	//属性值列表
	function attrvaluelist(){
		$this->global_list();
		$this->display();
	}
	
	//添加属性值 
	function addattrvalue(){
		$this->global_addoredit();
		$this->display();
	}
	
	//编辑属性值
	function editattrvalue(){
		$this->global_addoredit();
		$this->edit();
	}
	
	//列表公共处理
	function global_list(){
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
		$this->assign('groupid', $id);
		$this->assign('attrname', D('Attribute')->getAttributePosition($id));
	}
	
	//添加、编辑操作公共处理
	function global_addoredit(){
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
		$this->assign('attrid', $id);
		$this->assign('attrname', D('Attribute')->getAttributePosition($id));
	}
	
	//属性选择 option
	function selectOption(){
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
		$this->assign('option', ($id == 0) ? array() : D('Attribute')->getAttribute($id));
		$this->success($this->fetch());
	}
	
	/*--页面按钮--*/
	
	function _button_attribute_index(){
		return array(array('action'=>'addgroup', 'icon'=>'add'));
	}
	
	function _button_attribute_addgroup(){
		return array('index');
	}
	
	function _button_attribute_editgroup(){
		return array('index');
	}
	
	function _button_attribute_attrlist(){
		return array('index', array('action'=>'addattr', 'icon'=>'add', 'params'=>array('id'=>isset($_GET['id']) ? intval($_GET['id']) : 0)));
	}
	
	function _button_attribute_addattr(){
		return array('index', array('action'=>'attrlist', 'icon'=>'list', 'params'=>array('id'=>isset($_GET['id']) ? intval($_GET['id']) : 0)));
	}
	
	function _button_attribute_editattr(){
		return array('index', array('action'=>'attrlist', 'icon'=>'list', 'href'=>$_SERVER['HTTP_REFERER']));
	}
	
	function _button_attribute_attrvaluelist(){
		return array('index', array('action'=>'addattrvalue', 'icon'=>'add', 'params'=>array('id'=>isset($_GET['id']) ? intval($_GET['id']) : 0)));
	}
	
	function _button_attribute_addattrvalue(){
		return array('index', array('action'=>'attrvaluelist', 'icon'=>'list', 'params'=>array('id'=>isset($_GET['id']) ? intval($_GET['id']) : 0)));
	}
	
	function _button_attribute_editattrvalue(){
		return array('index', array('action'=>'attrvaluelist', 'icon'=>'list', 'href'=>$_SERVER['HTTP_REFERER']));
	}
}
?>